<template>
  <div >
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/admin' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/admin/user/list' }">用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>编辑用户</el-breadcrumb-item>
    </el-breadcrumb>
    <div class="edit-user">
      <h3>编辑用户</h3>
      <el-form :model="form" :rules="rules" ref="editUser" label-width="80px">
        <el-form-item label="昵称"  prop="name">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="Email"  prop="email">
          <el-input v-model="form.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码"  prop="password">
          <el-input v-model="form.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色"  prop="roles">
          <el-select v-model="form.roles" multiple placeholder="请选择">
            <el-option
              v-for="item in roles"
              :key="item.id"
              :label="item.display_name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" >
          <el-switch
            v-model="form.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="0"
          >
          </el-switch>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="update">更新</el-button>
          <el-button @click="reset">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
  export default {
    name: "UserEdit",
    data(){
      return {
        form:{
          status:1
        },
        roles:[],
        id:this.$route.params.id,
        rules:{
          name:[
            { required: true, message: '请输入昵称', trigger: 'blur' },
            { min: 6, max: 30, message: '长度在 6 到 30 个字符', trigger: 'blur' }
          ],
          email:[
            { required: true, message: '请输入邮箱', trigger: 'blur' },
            { type:'email', message: '请输入合法email', trigger: 'blur' },
            { min: 3, max: 100, message: '长度在 3 到 100 个字符', trigger: 'blur' }
          ],
          password:[
            { min: 6, max: 10, message: '长度在 6 到 10 个字符', trigger: 'blur' }
          ]
        },
      }
    },
    created(){
      this.getUser();
      this.allRoles();
    },
    methods:{
      update(){
        this.$refs.editUser.validate(valid =>{
          if(valid){
            this.axios.put(`/api/admin/user/${this.id}`,this.form).then(res => {
              this.$message({
                type: 'success',
                message: '操作成功!'
              });
              //this.form = {status:1}; // 清空表单数据
              // this.$refs.addUser.resetFields()
            }).catch(err => {
              this.$message.error(err.response.data.status)
            });
          }
          else{
            this.$message.error("请填写完整信息")
          }
        });
      },
      allRoles(){
        this.axios.get('/api/admin/role/all-roles').then(res=>{
          this.roles = res.data.data;
        }).catch(err=>{
          this.$message.error(err.response.data.status)
        });
      },
      getUser(){
        this.axios.get(`/api/admin/user/${this.id}`).then(res=>{
          this.form = res.data.data;
          this.form.roles = res.data.data.roles.map(row=>{return row.id})
        }).catch(err=>{
          this.$message.error(err.response.data.message)
        });
      },
      reset(){
        this.$router.push('/admin/user/list')
      }
    }
  }
</script>

<style scoped lang="less">
  .add-user{
    width: 400px;
    margin-top: 20px;
  }
</style>
